package com.zynote.nfhoujie.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import com.zynote.commons.base.BaseServiceHandler;
import com.zynote.commons.utils.SqlConditionObject;
import com.zynote.nfhoujie.dao.IUserDAO;
import com.zynote.nfhoujie.dao.IUserInfoDAO;
import com.zynote.nfhoujie.model.User;
import com.zynote.nfhoujie.model.UserInfo;
import com.zynote.nfhoujie.service.IUserService;

/**
 * 
 * @className:UserServiceImpl.java
 * @classDescription:用户服务层实现类
 * @author: AIDAN SU
 * @createTime: 2012-10-13
 *
 */

@Service
public class UserServiceImpl extends BaseServiceHandler implements IUserService {
	
	@Resource
	private IUserDAO userDAO ; 
	
	@Resource
	private IUserInfoDAO userInfoDAO ;

	@Override
	public User findUserById(Long id) {
		return userDAO.findUserById(id);
	}
	
	@Override
	public Long createUser(User user) {
		return userDAO.createUser(user);
		
	}
	
	@Override
	public Long createUserAndUserInfo(User user, UserInfo userInfo) {
		Long uid = userDAO.createUser(user);
		userInfo.setUid(uid);
		userInfoDAO.createUserInfo(userInfo);
		return uid;
	}

	@Override
	public void updateUser(User user) {
		userDAO.updateUser(user);
		
	}
	
	@Override
	public void deleteUser(User user) {
		userDAO.deleteUser(user);
		
	}
	
	@Override
	public void deleteUserById(Long id) {
		userDAO.deleteUserById(id);
	}

	@Override
	public User findUserByUsername(String username) {
		return userDAO.findUserByUsername(username);
	}

	@Override
	public User findUserByUsernameAndPassword(String username, String password, int userType) {
		return userDAO.findUserByUsernameAndPassword(username, password, userType);
	}

	@Override
	public int countUsers(Map<String, String[]> params) {
		return userDAO.countUsers( super.getSqlCondition(params) );
	}

	@Override
	public List<User> listUsers(int startPosition, int pageSize, Map<String, String[]> params) {
		return userDAO.listUsers(startPosition, pageSize,  super.getSqlCondition(params), getUserOrderProperties() );
	}
	
	/**
	 * 用户列表按属性排序
	 * @return
	 */
	private Map<String,String> getUserOrderProperties(){
		Map<String,String> orderProperties = new HashMap<String,String>();
		orderProperties.put("id", "desc");		
		return orderProperties;
	}

	@Override
	protected List<SqlConditionObject> getSqlConditionList( Map<String, String[]> params ) {
		
		List<SqlConditionObject> sqlConditionList  = new ArrayList<SqlConditionObject>();
		String[] username = params.get("username");
		String[] telphone = params.get("telphone");
		
		//用户名
		if( username != null && username.length > 0 && !"".equals( username[0].toString().trim() )){
			SqlConditionObject nameCondition = new SqlConditionObject();
			//条件语句的字段名（属性名）
			nameCondition.setKey("username");
			//条件语句的运算符号
			nameCondition.setSymbol("like");
			//条件语句的与或
			nameCondition.setType("and");
			//条件语句的值
			nameCondition.setValues(username);
			
			sqlConditionList.add(nameCondition);
		}
		
		//电话号码
		if( telphone != null && telphone.length > 0 && !"".equals( telphone[0].toString().trim() )){
			SqlConditionObject nameCondition = new SqlConditionObject();
			//条件语句的字段名（属性名）
			nameCondition.setKey("telphone");
			//条件语句的运算符号
			nameCondition.setSymbol("=");
			//条件语句的与或
			nameCondition.setType("and");
			//条件语句的值
			nameCondition.setValues(telphone);
			
			sqlConditionList.add(nameCondition);
		}
		
		
		return sqlConditionList;
	}

}
